package com.hansen.library.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hansen.library.listener.OnRecyclerItemClickListener;
import com.hansen.library.listener.OnRecyclerItemLongClickListener;

/**
 * @author han
 * @date 2019/12/17
 */
public abstract class BaseRecyclerBannerAdapter <VH extends BaseRecyclerBannerAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    public final Context context;

    private OnRecyclerItemClickListener onItemClickListener;
    private OnRecyclerItemLongClickListener onItemLongClickListener;

    public BaseRecyclerBannerAdapter(Context context) {
        this.context = context;
    }

    public void setOnItemClickListener(OnRecyclerItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnRecyclerItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = View.inflate(context, R.layout.item_horizontal, parent);
        View view = LayoutInflater.from(context).inflate(getViewResourceId(viewType), parent, false);
        return getViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        setView(holder, position);
    }

    public abstract Object getItem(int position);

    public abstract void setView(VH holder, int position);

    public abstract int getViewResourceId(int viewType);

    public abstract int getItemCountReal();

    public abstract VH getViewHolder(View view, int viewType);

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(ViewHolder.this);
                    }
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return onItemLongClickListener == null || onItemLongClickListener.onItemLongClick(ViewHolder.this);
                }
            });
        }
    }
}
